package com.nuanshui.heatedloan.extension;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


public class CustomHttpRequest extends HttpServletRequestWrapper {
	
	private  HttpServletResponse  response;
	public CustomHttpRequest(HttpServletRequest request,HttpServletResponse response) {
		super(request);
		this.response = response;
	}
	
	@Override
	public HttpSession getSession() {
		return super.getSession();
		
		// TODO Auto-generated method stub
		/*HttpSession session = super.getSession();
		String  uri = super.getRequestURI();
		if(uri.contains("signin_check")) {
			if(session!= null && !session.isNew()) {
				session.invalidate();
				session = super.getSession(true);
			}
			
		}
		if(session !=null&&session.isNew()&&!response.containsHeader("LBSESSIONID")) {
			response.addHeader("LBSESSIONID", session.getId());
		}
		return session;*/
	}
	
	@Override
	public HttpSession getSession(boolean create) {
		HttpSession session =  super.getSession(create);
		String  uri = super.getRequestURI();
		if(uri.contains("signin_check")) {
			if(session!= null && !session.isNew()) {
				session.invalidate();
				session = super.getSession(true);
			}
			
		}
		try {
			if(session !=null&&session.isNew()&&!response.containsHeader("LBSESSIONID")) {
				response.addHeader("LBSESSIONID", session.getId());
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		return session;
	}
	
	@Override
	public String[] getParameterValues(String name) {
		String[] strings =  super.getParameterValues(name);
		if(strings == null) {
			strings = super.getParameterValues(name+"[]");
		}else {
			if(strings.length==1 && strings[0].contains(",")) {
				strings = strings[0].split(",");						
			}			
		}
		return  strings;
	}
		
	@Override
	public String getRemoteAddr() {
		String  remoteAddr = super.getHeader("X-Real-IP");
		if(remoteAddr != null) return remoteAddr;
		return super.getRemoteAddr();
	}
	

	
}
